|
|
|
|
|
|
|
The Windows API, or Application Programming Interface, is a set of functions that Windows exposes to programmers. These Windows operating system functions can be called from Visual Basic to perform tasks that cannot be programmed with standard VB code. For example, standard Visual Basic does not have a function to reboot the computer. However, rebooting can be accomplished through a Windows API call. You can think of many of the functions included with Visual Basic as "wrappers" around the underlying Windows API routines. Using Windows API functions is a bit more complicated than using standard user-defined functions, so writing your own wrapper functions is common. Although by no means an exhaustive guide, this chapter provides a solid foundation for common uses of the Windows API. |
|
|
|
|
|
|
|
|
Understanding the Windows API |
|
|
|
|
|
|
|
|
From the point of view of the Visual Basic programmer, Windows API functions can be thought of as similar to "normal" VB functions. They contain input and output parameters and sometimes a return value. However, API functions are already compiled in a separate file, known as a dynamic link library (or DLL). |
|
|
|
|
|
|
|
|
To use these functions, you have to add a few lines of code that define the external function for Visual Basic. In other words, to use an API function, you must first declare it. API declarations are entered in the General Declarations section of a module. Just as you declare variables, you also must declare API functions so that your code can access them; and, as a declared variable provides a link to a memory location, an API declaration provides a link to an external DLL. |
|
|
|
|
|
|
|
|
|
The usual place for API declarations is in a module, but you can add them to forms and classes as well by adding the Private keyword in front of the declaration.  |
|
|
|
|
|
|
|
|
|
|
The Declare statement lists all the parameters of the API function, the DLL in which it is located, and the data type of the return value. Unlike a normal VB function, an API declaration has no function code. Instead, the single-line statement merely points to the DLL file containing the function. This relationship is illustrated in Figure 20.1. |
|
|
|
|
|
|
|
|
Figure 20.1
API functions are stored in system DLLs. The API
declaration makes these fuctions visible to your VB code. |
|
|
|
|
|
|
|
|
|
Many Declare statements include the use of the Alias parameter. An alias specifies the "real" name of an API function as it is stored in the DLL, which may not match the name you want to give the function in your program. For example, a function named _lopen appears in the kernel32 DLL, but _lopen is not a valid function name within Visual Basic. In this case, the proper API declaration is as follows: |
|
|
|
|
|
|
|